package cn.kinoko.common.utils;

import java.security.SecureRandom;
import java.util.Random;


/**
 * @author kinoko
 */
public class RandomStrGenerator {

    private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
    private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase();
    private static final String NUMBER = "0123456789";
    private static final String ALL_CHARS = CHAR_LOWER + CHAR_UPPER + NUMBER;

    public static String generate(int length) {
        if (length < 1) {
            throw new IllegalArgumentException("Length must be at least 1");
        }
        // 更安全的随机数生成器
        Random random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);

        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(ALL_CHARS.length());
            char randomChar = ALL_CHARS.charAt(randomIndex);
            sb.append(randomChar);
        }

        return sb.toString();
    }

    public static void main(String[] args) {
        int length = 10;
        String randomStr = generate(length);
        System.out.println("Generated random string of length " + length + ": " + randomStr);
    }
}
